package com.aplana.ccsd.jrebel;

import info.magnolia.init.MagnoliaConfigurationProperties;
import info.magnolia.init.MagnoliaServletContextListener;
import info.magnolia.init.PropertySource;
import info.magnolia.objectfactory.Components;

import java.lang.reflect.Field;
import java.util.Properties;

public class JRebelEnablingMagnoliaServletContextListener extends
		MagnoliaServletContextListener {
	
	@Override
	protected void startServer() {
		try {
			String propertyName = "com.zeroturnaround.bundled.org.apache.commons.logging.Log";
			PropertySource propertySource = Components.getComponent(
					MagnoliaConfigurationProperties.class).getPropertySource(
					propertyName);
			Field field = propertySource.getClass().getSuperclass()
					.getDeclaredField("properties");
			field.setAccessible(true);
			Properties properties = (Properties) field.get(propertySource);
			properties.remove(propertyName);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}
		super.startServer();
	}
}